<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html>
<head>
<title>Dialogs in Java Gnome</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="GUI, Java Gnome, dialogs, Java, programming, Linux">
<meta nam="description" content="Dialogs in Java Gnome">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<div class="content">


<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>


<h1>Dialogs in Java Gnome</h1>


<p>
In this part of the Java Gnome programming tutorial, we will introduce dialogs. 
</p>

<div class="center">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-9706709751191532";
/* top_horizontal */
google_ad_slot = "3327173442";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<p>
Dialog windows or dialogs are an indispensable part of most modern GUI applications.
A dialog is defined as a conversation between two or more persons. In a computer
application a dialog is a window which is used to "talk" to the application.
A dialog is used to input data, modify data, change the application settings etc.
Dialogs are important means of communication between a user and a computer program.
</p>


<h2>Message dialogs</h2>

<p>
Message dialogs are convenient dialogs that provide messages 
to the user of the application. The message consists of 
textual and image data. 
</p>

<div class="codehead">messages.java</div>
<pre class="code">
package com.zetcode;

import org.gnome.gdk.Event;
import org.gnome.gtk.Button;
import org.gnome.gtk.ButtonsType;
import org.gnome.gtk.Gtk;
import org.gnome.gtk.MessageDialog;
import org.gnome.gtk.MessageType;
import org.gnome.gtk.Table;
import org.gnome.gtk.Widget;
import org.gnome.gtk.Window;
import org.gnome.gtk.WindowPosition;


/**
 * Java Gnome tutorial
 *
 * This program shows four
 * message dialogs.
 *
 * @author jan bodnar
 * website zetcode.com
 * last modified March 2009
 */


public class GMessages extends Window {

    Window parent;

    public GMessages() {
    
        setTitle("GMessages");
        
        initUI();
        
        parent = this;
        
        connect(new Window.DeleteEvent() {
            public boolean onDeleteEvent(Widget source, Event event) {
                Gtk.mainQuit();
                return false;
            }
        });
        
        setDefaultSize(250, 100);
        setPosition(WindowPosition.CENTER);
        showAll();
    }
    
    
    public void initUI() {
    
        Table table = new Table(2, 2, true);
        
        Button info = new Button("Information");
        Button warn = new Button("Warning");
        Button ques = new Button("Question");
        Button erro = new Button("Error");

        info.connect(new Button.Clicked() {

            public void onClicked(Button button) {
                MessageDialog md = new MessageDialog(null, true,
                     MessageType.INFO, 
                    ButtonsType.CLOSE, "Download completed");
                md.setPosition(WindowPosition.CENTER);
                md.run();
                md.hide();
            }
        });

        warn.connect(new Button.Clicked() {
        
            public void onClicked(Button button) {
                MessageDialog md = new MessageDialog(parent, true,
                    MessageType.WARNING, 
                    ButtonsType.CLOSE, "Unallowed operation");
                md.setPosition(WindowPosition.CENTER);
                md.run();
                md.hide();
            }
        });
        
        
        ques.connect(new Button.Clicked() {
        
            public void onClicked(Button button) {
                MessageDialog md = new MessageDialog(null, true,
                    MessageType.QUESTION, 
                    ButtonsType.CLOSE, "Are you sure to quit?");
                md.setPosition(WindowPosition.CENTER);
                md.run();
                md.hide();
            }
        });
        
        erro.connect(new Button.Clicked() {
            public void onClicked(Button button) {
                MessageDialog md = new MessageDialog (null, true, 
                    MessageType.ERROR, 
                    ButtonsType.CLOSE, "Error loading file");
                md.setPosition(WindowPosition.CENTER);
                md.run();
                md.hide();
            }            
         });

        table.attach(info, 0, 1, 0, 1);
        table.attach(warn, 1, 2, 0, 1);
        table.attach(ques, 0, 1, 1, 2);
        table.attach(erro, 1, 2, 1, 2);
        
        add(table);
    }
    
    
    public static void main(String[] args) {
        Gtk.init(args);
        new GMessages();
        Gtk.main();
    }
}
</pre>

<p>
In our example, we will show four kinds of message dialogs. 
Information, Warning, Question and Error message dialogs.
</p>

<pre class="explanation">
 Button info = new Button("Information");
 Button warn = new Button("Warning");
 Button ques = new Button("Question");
 Button erro = new Button("Error");
</pre>

<p>
We have four buttons. Each of these buttons will show a different kind of message dialog. 
</p>

<pre class="explanation">
 info.connect(new Button.Clicked() {

     public void onClicked(Button button) {
     MessageDialog md = new MessageDialog(null, true,
       MessageType.INFO, 
         ButtonsType.CLOSE, "Download completed");
     md.setPosition(WindowPosition.CENTER);
     md.run();
     md.hide();
     }
 });
</pre>

<p>
If we click on the info button, the Information dialog is displayed. 
The <b class="keyword">MessageType.INFO</b> specifies the type of the
dialog. The <b class="keyword">ButtonsType.CLOSE</b> specifies the button to 
be displayed in the dialog. The last parameter is the message dislayed. 
The dialog is displayed with the <b class="keyword">run()</b> method. 
The <b class="keyword">hide()</b> method hides the dialog. 
</p>


<br>
<img src="/img/gui/javagnome/information.png" alt="Information message dialog">&nbsp;&nbsp;
<img src="/img/gui/javagnome/warning.png" alt="Warning message dialog">&nbsp;&nbsp;
<img src="/img/gui/javagnome/question.png" alt="Question message dialog">&nbsp;&nbsp;
<img src="/img/gui/javagnome/error.png" alt="Error message dialog">
<br>
<br>


<h2>AboutDialog</h2>

<p>
The <b class="keyword">AboutDialog</b> displays information about the application. 
<b class="keyword">AboutDialog</b> can display a logo, the name of the application, 
version, copyright  or licence information. 
It is also possible to give credits to the authors or translators. 
</p>

<div class="codehead">aboutdialog.java</div>
<pre class="code">
package com.zetcode;

import java.io.FileNotFoundException;

import org.gnome.gdk.Event;
import org.gnome.gdk.Pixbuf;
import org.gnome.gtk.AboutDialog;
import org.gnome.gtk.Button;
import org.gnome.gtk.Fixed;
import org.gnome.gtk.Gtk;
import org.gnome.gtk.Widget;
import org.gnome.gtk.Window;
import org.gnome.gtk.WindowPosition;


public class GAboutDialog extends Window implements Button.Clicked {

    Pixbuf logo;

    public GAboutDialog() {
    
        setTitle("AboutDialog");
    
        initUI();
        
        connect(new Window.DeleteEvent() {
            public boolean onDeleteEvent(Widget source, Event event) {
                Gtk.mainQuit();
                return false;
            }
        });
        
        setDefaultSize(250, 200);
        setPosition(WindowPosition.CENTER);
        showAll();
    }
    
    
    public void initUI() {
    
        try {
            logo = new Pixbuf("battery.png");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    
        Button button = new Button("About");
        button.connect(this);

        Fixed fix = new Fixed();
        fix.put(button, 20, 20);
        add(fix);

    }
    
    public void onClicked(Button button) {
        AboutDialog about = new AboutDialog();
        about.setProgramName("Battery");
        about.setVersion("0.1");
        about.setCopyright("(c) Jan Bodnar");
        about.setComments("Battery is a simple tool for battery checking");
        about.setLogo(logo);
        about.setPosition(WindowPosition.CENTER);
        about.run();
        about.hide();
    }

    public static void main(String[] args) {
        Gtk.init(args);
        new GAboutDialog();
        Gtk.main();
    }
}
</pre>

<p>
The code example uses a <b class="keyword">AboutDialog</b> with some of it's features. 
</p>

<pre class="explanation">
 AboutDialog about = new AboutDialog();
</pre>

<p>
We create an <b class="keyword">AboutDialog</b>. 
</p>

<pre class="explanation">
 about.setProgramName("Battery");
 about.setVersion("0.1");
 about.setCopyright("(c) Jan Bodnar");
</pre>

<p>
We set the name, version and the copyright.
</p>

<pre class="explanation">
 about.setLogo(logo);
</pre>

<p>
This line creates a logo. 
</p>


<br>
<img src="/img/gui/javagnome/battery.png" alt="AboutDialog">
<div class="figure">Figure: AboutDialog</div>
<br>

<hr class="btm">

<p>
In this part of the Java Gnome tutorial, we have covered Dialogs. 
</p>

<br>
<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div> 
<br>

<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡ 
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified March 12, 2009  <span class="copyright">&copy; 2007 - 2012 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>
